<?php


namespace app\common\service\member;


class WxService
{
    //根据code获取openid

    public static function getOpenid($code){
        $appId = 'wx566f27e57c6e058b';
        $appSecret = '7909cde123df86f021552008326c351b';
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
        $response = file_get_contents($url);
        $data = json_decode($response, true);
        // 检查是否成功获取
        if (isset($data['openid'])) {
            return $data['openid'];
        } else {
            exception("Error: " . $data['errmsg']  . $data['errcode']);
        }
    }
}
